User blog:SlackerPrime/Per-Pixel Text Scroller pt. 1 - Get that string scrollin'!
EDIT Feb 6 2015: Added a VSYNC 1 to @LOOP, forgot GCLS in @DRAW. Alright, here we are. Our text scroller is going to be drawn on the graphic screen using GPUTCHR. If you haven't used it, it basically draws any graphic from one of the given graphic banks (including the font!) onto the graphic screen in a given position, palette, and scale. Buuuut before we do that we need to set up our program! This is what the init code looks like, including a skeleton of our main loop: ACLS CLEAR PA=0 PB=1 GPAGE 0,PA,PB SCALE=2 PAL=0 TEXTX=256 TEXTY=95 SPEED=2 ST$="Hello!" CSY=TEXTY-((8*SCALE)/2) CEY=CSY+((8*SCALE)-1) DIM ST(LEN(ST$)) FOR I=0 TO LEN(ST$)-1 ST(I)=ASC(MID$(ST$,I,1)) NEXT @LOOP VSYNC 1 GOSUB @SCROLL GOSUB @DRAW GOSUB @FLIP GOTO @LOOP There's a very good reason everything is packed into a central control loop: flexibility and simplicity. It makes everything easier to edit and control. You might also notice this block up here: PA=0 PB=1 GPAGE 0,PA,PB Here, we're just setting up a page-flip to buffer the graphics page. At the end of the loop, we call the @FLIP subroutine to flip the graphics pages and present the graphics like this: @FLIP SWAP PA,PB GPAGE 0,PA,PB RETURN PA and PB are constants representing the two graphics pages to use. SCALE is the scale factor we use to draw the characters with GPUTCHR. SPEED is how many pixels the text scrolls each frame. TEXTX and TEXTY are two anchors for drawing the text, but they act a little bit differently. TEXTX is the x-coordinate of the beginning of the string, but TEXTY is the y-coordinate in the ''center ''of the string. In this program, we'll be drawing the string at CSY (copy start y). In the FOR loop, we read in the string data en masse into an array ST(). This array holds the ASCII codes for the characters in the string, which we'll use to draw the string characters. We have our string loaded up, so now we can draw it. Here's our @DRAW subroutine. It's a bit strange, but here we go: @DRAW GCLS FOR I=0 TO LEN(ST$)-1 GPUTCHR TEXTX+((8*SCALE)*I),CSY,"BGF",ST(I),PAL,SCALE NEXT RETURN GPUTCHR might be unfamiliar to you guys, so here's its syntax: GPUTCHR xCoordinate,yCoordinate,characterBank$,characterNumber,paletteNumber,scale characterBank$ is a string telling what character page to load characterNumber from. We're using the font characters for this, so we use "BGF". The x-coordinate is offset relative to TEXTX depending on what character we're drawing, so all of the characters get positioned properly. Finally, we have to write the @SCROLL subroutine, which is just this: @SCROLL TEXTX=TEXTX-SPEED RETURN Honestly, there's no good reason to put @SCROLL in it's own subroutine, but we're not going to be optimizing anything here. Just leave it. Run this bad boy, and you can see the text "Hello!" scrolling by all smooth-like. Here's the full source if you didn't catch something: ACLS CLEAR PA=0 PB=1 GPAGE 0,PA,PB SCALE=2 PAL=0 TEXTX=256 TEXTY=95 SPEED=2 ST$="Hello!" CSY=TEXTY-((8*SCALE)/2) CEY=CSY+((8*SCALE)-1) DIM ST(LEN(ST$)) FOR I=0 TO LEN(ST$)-1 ST(I)=ASC(MID$(ST$,I,1)) NEXT @LOOP VSYNC 1 GOSUB @SCROLL GOSUB @DRAW GOSUB @FLIP GOTO @LOOP @DRAW GCLS FOR I=0 TO LEN(ST$)-1 GPUTCHR TEXTX+((8*SCALE)*I),CSY,"BGF",ST(I),PAL,SCALE NEXT RETURN @SCROLL TEXTX=TEXTX-SPEED RETURN @FLIP SWAP PA,PB GPAGE 0,PA,PB RETURN In the next lesson, we'll be learning how to put a cool effect on the text. Category:Blog posts